#!/usr/bin/evn python
# -*- coding: utf-8 -*-

from typing import MutableMapping, Any, Union

import os

from infirmary_admin_src.infirmary_smart_admin.infirmary_settings.base import ISettings
from infirmary_admin_src.infirmary_smart_admin.infirmary_settings.development import DevSettings
from infirmary_admin_src.infirmary_smart_admin.infirmary_settings.production import ProSettings


class DevSettingsBuilder():
    ''' 开发环境的配置 '''

    @classmethod
    def build(cls) -> ISettings:
        return DevSettings()


class ProSettingsBuilder():
    ''' 开发环境的配置 '''

    @classmethod
    def build(cls) -> ISettings:
        return ProSettings(environment='production')


class Environment():
    '''
    该静态方法实现了从环境变量中读取设置，并将其转换为Settings类型，以便使用。
    '''

    @staticmethod
    def select() -> Union[DevSettings, ProSettings, ISettings]:
        '''
        定义静态方法select，它返回一个Settings类型的值；
        :return:
        '''
        # 根据环境变量的来选择使用的配置
        env_vars: MutableMapping[Any, Any] = os.environ
        print("环境变量信息", env_vars)
        # 根据不同环境变量返回不同的环境配置实例对象
        if env_vars.get('environment') == 'development':
            settings_class = DevSettingsBuilder
        # 如果是正式环境则使用正式环境的配置
        elif env_vars.get('environment') == 'production':
            settings_class = DevSettingsBuilder
        else:
            # 默认的是要开发环境的配置信息
            settings_class = DevSettingsBuilder
        # 创建配置实例对象
        settings = settings_class().build()
        return settings
